/*张欢欢，费用管理页面js*/
var totalPage;
var totalCnt;
var chargingCost = [];
var deleteFlag;
$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	selectDate("date");
	setTimeout(hideLoading, 500);
});

function findFirstPage(){
	$("#page").val(1);
	$("#currentPage").val(1);
	search();
}
/*费用管理查询*/
function search(){
	showLoading();
	seach = 1;
	$("#chkAll").attr("checked", false);
	$.ajax({
		type: "POST",
		url: 'searchChargingCost',
		dataType:'json',                  
		data : $("#searchChargingCost").serialize(),		
		success: function(data){
			setTimeout(hideLoading, 500);
			clearTable("chargingCostTbody");
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			chargingCost = data && data.chargingCostsList || [];
			//当前页数
			var currentPage = $('#currentPage').val();
			var rows=$("#rows").val();
			document.getElementById("total").innerText = '共' + totalCnt + '条 / 共' + totalPage + '页';
			//对应的表格ID
			var tbody=$("#chargingCostTbody");
			var length = chargingCost.length;
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
				for(var i=0; i<length; i++){
					var $tr = $('<tr>').appendTo(tbody);
					var rowNo = (currentPage - 1) * rows + i + 1;
					$("<td>").html("<input type='checkbox' name='chk' id='chk' >").appendTo($tr);
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(date2String(chargingCost[i].effectiveDate)).appendTo($tr);
					$("<td>").html(chargingCost[i].chargePrice).appendTo($tr);
					$("<td>").html(chargingCost[i].ratio).appendTo($tr);
					$("<td>").html(chargingCost[i].monthKWH).appendTo($tr);
					$("<td>").html(chargingCost[i].servicePrice).appendTo($tr);
					$("<td>").html(chargingCost[i].oilPrice).appendTo($tr);
				}
			}
		},
		error: function(data){
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("没有访问权限");
			} else {
				showMessge("查询失败!");
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}
function save() {
	$("#oilPrice").css("display","block");
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '充电费用信息添加';
	selectDate("saveDate");
	$("#saveEffectiveDate").attr("disabled", false);
	$("#saveChargePrice").attr("disabled", false);
	$("#saveRatio").attr("disabled", false);
	$("#saveMonthKWH").attr("disabled", false);
	$("#saveServicePrice").attr("disabled", false);
	$("#saveOilPrice").attr("disabled", false);
	$("#save").modal("show");
	deleteFlag = "save";
	saveTitle.append(strong);
    formReset("saveForm");
}
function update() {
	//日期显示
	$("#effectiveDate").css("display", "block");
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '充电费用信息修改';
	//当日油价隐藏
//	$("#oilPrice").css("display","none");
	if (count == 1) {
		$('#save').modal('show');
		$("#saveEffectiveDate").val(date2String(chargingCost[index].effectiveDate)).attr("disabled",true);
		$("#saveChargePrice").val(chargingCost[index].chargePrice);
		$("#saveRatio").val(chargingCost[index].ratio);
		$("#saveMonthKWH").val(chargingCost[index].monthKWH);
		$("#saveServicePrice").val(chargingCost[index].servicePrice);
		$("#saveOilPrice").val(chargingCost[index].oilPrice);
	} else if (count == 0) {
		showMessge("请选择要操作的数据!");
	} else {
		showMessge("抱歉,不能选择多行!");
	}
	deleteFlag = "update";
	saveTitle.append(strong);
}
function confirm() {
	var saveEffectiveDate = $("#saveEffectiveDate").val();
	var saveChargePrice = $("#saveChargePrice").val();
	var saveRatio = $("#saveRatio").val();
	var saveMonthKWH = $("#saveMonthKWH").val();
	var saveServicePrice = $("#saveServicePrice").val();
	var saveOilPrice = $("#saveOilPrice").val();
	if (!saveEffectiveDate) {
		showMessge("费用管理日期不能为空");
		return;
	}
	if (!saveChargePrice) {
		showMessge("电价不能为空");
		return;
	}
	if (!saveServicePrice) {
		showMessge("当日充电服务费不能为空");
		return;
	}
	if (!saveMonthKWH) {
		showMessge("贸易表本月用电量不能为空");
		return;
	}
	if (!saveRatio) {
		showMessge("系数不能为空");
		return;
	}
	if (deleteFlag == "save") {
		if (!saveOilPrice) {
			showMessge("油价不能为空");
			return;
		}
		showLoading();
		$.ajax({
			type: "POST",
			url: 'saveCharingCost',
	    	data: {
	    		ChargingCostDate : saveEffectiveDate,
	    		ChargePrice : saveChargePrice,
	    		ServicePrice : saveServicePrice,
	    		MonthKWH : saveMonthKWH,
	    		Ratio : saveRatio,
	    		OilPrice : saveOilPrice
	    	},
			dataType:'json',
			success: function(data){
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("添加成功");
				} else if (result == "NotFound") {
					showMessge("添加失败");
				} else if(result == "ChargingStationIDNoNull"){
					showMessge("充电站的费用管理日期已存在");
				} else if(result == "EffectiveDateNoNull"){
					showMessge("有效日期已存在，请重新输入");
				} else{
					showMessge("添加异常");
				}
				search();
				formReset("saveForm");
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!");
				} else {
					showMessge("添加失败!");
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		});
	 } else if (deleteFlag == "update") {
		 showLoading();
		 $.ajax({
			type: "POST",
			url: 'updateCharingCost',
		    	data: {
		    		ChargingCostDate : saveEffectiveDate,
		    		ChargePrice : saveChargePrice,
		    		Ratio : saveRatio,
		    		MonthKWH : saveMonthKWH,
		    		ServicePrice : saveServicePrice,
		    		OilPrice : saveOilPrice
		    	},
			dataType:'json',
			success : function(data) {
				setTimeout(hideLoading, 500);
				$('#save').modal('hide');
				var result = data && data.result;
				if (result == "Success") {
					showMessge("修改成功");
				} else if (result == "NotFound") {
					showMessge("修改失败");
				} else if (result == "ChargingStationIDNoNull") {
					showMessge("充电站ID已存在，请重新输入");
				} else if (result == "EffectiveDateNoNull") {
					showMessge("有效日期已存在，请重新输入");
				} else if (result == "Error") {
					showMessge("修改异常");
				}
				search();
				formReset("saveForm");
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("您没有操作权限!");
				} else {
					showMessge("操作失败!");
				}
			},
			timeout: function(data) {
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		});
	}
}
//费用管理 删除
function sure() {
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count || 0;
	var index = undefined;
	var effectiveDate = undefined;
	var effectiveDateList = [];
	for (var i = 0; i < count; i++) {
		index = checkbox && checkbox.row_index[i] || 0;
		effectiveDate = chargingCost && chargingCost[index] && date2String(chargingCost[index].effectiveDate);
		effectiveDateList.push(effectiveDate);
	}
	$.ajax({
		type : "post",
		url : "deleteCharingCost",
		dataType : "json",
		data : {
			effectiveDateList : effectiveDateList.toString()
		},
		success : function(data) {
			setTimeout(hideLoading, 500);
			var result = data && data.result;
			if (result == "Success") {
				showMessge("删除成功");
			} else if (result == "NotFound") {
				showMessge("删除数据不存在");
			} else {
				showMessge("删除失败");
			}
			search();
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("没有访问权限!");
			} else {
				showMessge("操作失败!");
			}
		},
		timeout: function(data) {
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}